package com.zhouyibike.uu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.Hehuorenpic;
import com.zhouyibike.uu.utils.Util;

import java.util.List;

/**
 * Created by cc on 2017/5/3.
 */

public class HehuorenjieshaoAdapter extends BaseAdapter {
    private Context context;
    private List<Hehuorenpic.DataBean> list;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;

    public HehuorenjieshaoAdapter(Context context, List<Hehuorenpic.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Hehuorenpic.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
//        return position == list.size() - 1 ? TYPE_2 : TYPE_1;
        return TYPE_1;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        ViewHolder2 holder2 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_rengoupic, null);
                    holder = new ViewHolder();
                    // 以下为保存这一屏的内容，供下次回到这一屏的时候直接refresh，而不用重读布局文件
                    holder.img = (ImageView) convertView.findViewById(R.id.iv_itempic_pic);
                    convertView.setTag(holder);
                    break;
                case TYPE_2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_rengoupicthelast, null);
                    holder2 = new ViewHolder2();
                    // 以下为保存这一屏的内容，供下次回到这一屏的时候直接refresh，而不用重读布局文件
                    holder2.img = (ImageView) convertView.findViewById(R.id.iv_itempic_pic);
                    convertView.setTag(holder2);
                    break;
            }
        }else{
            switch (type) {
                case TYPE_1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_2:
                    holder2 = (ViewHolder2) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
//                .memoryPolicy(MemoryPolicy.NO_CACHE)
                Util.setLviewwh(holder.img,Double.parseDouble(list.get(position).getWidth()),Double.parseDouble(list.get(position).getHeight()),1);
            //    Picasso.with(context).setIndicatorsEnabled(true);
                Picasso.with(context).load(list.get(position).getImagePath()).placeholder(R.drawable.zhanweitu).error(R.drawable.zhanweitu)
                        .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE) .into(holder.img);  // 加载网络图片
                break;
            case TYPE_2:
                Util.setLviewwh(holder2.img,Double.parseDouble(list.get(position).getWidth()),Double.parseDouble(list.get(position).getHeight()),1);
                Picasso.with(context).load(list.get(position).getImagePath()).placeholder(R.drawable.zhanweitu).error(R.drawable.zhanweitu)
                        .networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE) .into(holder2.img);  // 加载网络图片
                break;
        }
        return convertView;
    }

    public final class ViewHolder {
        public ImageView img;
    }

    public final class ViewHolder2 {
        public ImageView img;
    }
}
